<?php

//后台商品控制器
//命名空间
namespace Kangtai\Controller;
use Think\Controller;
class KangtaiController extends Controller{


	//轮播图接口
	public function Banner(){
		$ad = D("ad");
		$ad = $ad -> field("ad_code") -> select() ;
		echo json_encode($ad); 
	}
	
	//商品类接口
	public function Goods(){
		
		$json = $GLOBALS['HTTP_RAW_POST_DATA'];
		$arrlist = json_decode($json, TRUE);
		switch ($arrlist['type']) {
			case 1:
					$pageNow = $arrlist['frequency']; 
					$pageSize = $arrlist['amount'];
					$start=($pageNow-1)*$pageSize;
					$sql="select * from tp_children_equipment  limit $start,$pageSize";
					

					$children_equipment = D("children_equipment");
				    
				    $shulinc = $children_equipment->count();
				   	$children_equipment = $children_equipment -> query($sql);

				   	
				   	if($shulinc%$pageSize == 0){
				   			$weiye = $shulinc/$pageSize;
				   	}else{
				   		$weiye =floor( $shulinc/$pageSize +1) ;
				   	}
				    	
				    	
				      foreach ($children_equipment as $k => $v) {
				        $arr[$k] = $v;
				        $arr[$k]['img'] = "https://project.csnet.net.cn".substr($v['img'], 8);

				   	 
					}	
					if($weiye<$pageNow){
						echo json_encode(1);
					}else{
						echo json_encode($arr);
					}

						
				

				break;
			case 2:
					
					$pageNow = $arrlist['frequency']; 
					$pageSize = $arrlist['amount'];
					$start=($pageNow-1)*$pageSize;
					$sql="select * from tp_adult_equipment  limit $start,$pageSize";
					
					$adult_equipment= D("adult_equipment");
     	   			$shulinc = $adult_equipment->count();
				   	$adult_equipment = $adult_equipment -> query($sql);
				   	if($shulinc%$pageSize == 0){
				   			$weiye = $shulinc/$pageSize;
				   	}else{
				   		$weiye =floor( $shulinc/$pageSize +1) ;
				   	}

     					 foreach ($adult_equipment as $k => $v) {
						        $arr[$k] = $v;
						        $arr[$k]['img'] = "https://project.csnet.net.cn".substr($v['img'], 8);
						    }
						 	

						if($weiye<$pageNow){
						echo json_encode(1);
					}else{
						echo json_encode($arr);
					}

				break;
			case 3:
					$pageNow = $arrlist['frequency']; 
					$pageSize = $arrlist['amount'];
					$start=($pageNow-1)*$pageSize;
					$sql="select * from tp_auxiliary_equipment  limit $start,$pageSize";		

					$auxiliary_equipment= D("auxiliary_equipment");
     	   			$shulinc = $auxiliary_equipment->count();
				   	$auxiliary_equipment = $auxiliary_equipment -> query($sql);
     	   			if($shulinc%$pageSize == 0){
				   			$weiye = $shulinc/$pageSize;
				   	}else{
				   		$weiye =floor( $shulinc/$pageSize +1) ;
				   	}


     					 foreach ($auxiliary_equipment as $k => $v) {
						        $arr[$k] = $v;
						        $arr[$k]['img'] = "https://project.csnet.net.cn".substr($v['img'], 8);
						    }
					if($weiye<$pageNow){
						echo json_encode(1);
					}else{
						echo json_encode($arr);
					}

					break;
			case 4:
					
     	   			$pageNow = $arrlist['frequency']; 
					$pageSize = $arrlist['amount'];
					$start=($pageNow-1)*$pageSize;
					$sql="select * from tp_training_equipment  limit $start,$pageSize";		

					$training_equipment= D("training_equipment");
     	   			$shulinc = $training_equipment->count();
				   	$training_equipment = $training_equipment -> query($sql);
     	   			if($shulinc%$pageSize == 0){
				   			$weiye = $shulinc/$pageSize;
				   	}else{
				   		$weiye =floor( $shulinc/$pageSize +1) ;
				   	}


     					 foreach ($training_equipment as $k => $v) {
						        $arr[$k] = $v;
						        $arr[$k]['img'] = "https://project.csnet.net.cn".substr($v['img'], 8);
						    }
					if($weiye<$pageNow){
						echo json_encode(1);
					}else{
						echo json_encode($arr);
					}
				break;

    
		}
	

	}

	//单个商品内容
	public function Goodsarr(){
		$json = $GLOBALS['HTTP_RAW_POST_DATA'];
		$arrlist = json_decode($json, TRUE);
		//echo json_encode($arrlist);
		switch ($arrlist['goodsType']) {
			case 1:
					$children_equipment = D("children_equipment");
				    $children_equipment = $children_equipment ->where("id = {$arrlist['goodsId']}") -> find();
				    $children_equipment['img'] = "https://project.csnet.net.cn".substr( $children_equipment['img'], 8);
				    echo json_encode($children_equipment);
				break;
			case 2:
					$adult_equipment= D("adult_equipment");
     	   			$adult_equipment = $adult_equipment-> where("id = {$arrlist['goodsId']}") -> find();
     				$adult_equipment['img'] = "https://project.csnet.net.cn".substr( $adult_equipment['img'], 8);	
     				echo json_encode($adult_equipment);
				break;
			case 3:
					$auxiliary_equipment= D("auxiliary_equipment");
     	   			$auxiliary_equipment = $auxiliary_equipment ->where("id = {$arrlist['goodsId']}") -> find();
     				$auxiliary_equipment['img'] = "https://project.csnet.net.cn".substr( $auxiliary_equipment['img'], 8);
     				echo json_encode($auxiliary_equipment);
				break;
			case 4:
					$training_equipment= D("training_equipment");
     	   			$training_equipment = $training_equipment ->where("id = {$arrlist['goodsId']}") -> find();
     				$training_equipment['img'] = "https://project.csnet.net.cn".substr( $training_equipment['img'], 8);	
     				echo json_encode($training_equipment);	
				break;

    
		}
	

	}
	//案例显示
	public function Casearr(){
		$json = $GLOBALS['HTTP_RAW_POST_DATA'];
		$arrlist = json_decode($json, TRUE);
		
		switch ($arrlist['goodsType']) {
			case 1: //1是客户动态
					$customer_case = D("customer_case");
	  	      		$customer_case = $customer_case -> select();
     	 			foreach ($customer_case as $k => $v) {
    					$customer_case[$k] = $v;
    					$customer_case[$k]['img'] = "https://project.csnet.net.cn".substr($v['img'], 8);
   				 	}
					echo json_encode($customer_case);	
				break;
			case 2: //2是设计案例
					$design_case = D("design_case");			  
				    $design_case = $design_case -> select();
				     	 foreach ($design_case as $k => $v) {
						        $design_case[$k] = $v;
						        $design_case[$k]['img'] = "https://project.csnet.net.cn".substr($v['img'], 8);
				   			 }
					echo json_encode($design_case);	
				break;

		}
	}
	//动态类显示
	public function Dynamemic(){
		$json = $GLOBALS['HTTP_RAW_POST_DATA'];
		$arrlist = json_decode($json, TRUE);
		switch ($arrlist) {
			case 1:
				# code...
				break;
			case 2:
				# code...
				break;
		
		}
	}

	//动态访问数添加
	public function DynamemicSave(){
		$json = $GLOBALS['HTTP_RAW_POST_DATA'];
		$arrlist = json_decode($json, TRUE);
		switch ($arrlist) {
			case 1:
				# code...
				break;
			case 2:
				# code...
				break;
		
		}
	}
	//动态企业新闻显示
	public function News(){
		$json = $GLOBALS['HTTP_RAW_POST_DATA'];
		$arrlist = json_decode($json, TRUE);
		switch ($arrlist['type']) {
			case 'companyNews':
				$article = M('article');
				$result = $article->where('cat_id=1')->field('article_id,title,thumb,add_time,click')->order('article_id desc')->limit(4)->select();

				foreach ($result as $k => $v) {
					$result[$k] = $v;
    				$result[$k]['add_time'] = date('Y-m-d',$result[$k]['add_time']);
				}
				echo json_encode($result);	
				break;
			case 'industryNews':
				$article = M('article');
				$result = $article->where('cat_id=2')->field('article_id,title,thumb,add_time,click')->order('article_id desc')->limit(4)->select();
				foreach ($result as $k => $v) {
					$result[$k] = $v;
    				$result[$k]['add_time'] = date('Y-m-d',$result[$k]['add_time']);
				}
				echo json_encode($result);	
				break;
		
		}
	}
	public function NewsDetail(){
		$json = $GLOBALS['HTTP_RAW_POST_DATA'];
		$arrlist = json_decode($json, TRUE);

		$id = $arrlist['id'];

		$article = M('article');
		$resut = $article->where('article_id='.$id)->field('content')->select();
		echo json_encode($resut);
	}
	public function NewsDataList(){
		$json = $GLOBALS['HTTP_RAW_POST_DATA'];
		$arrlist = json_decode($json, TRUE);

		switch ($arrlist['type']) {
			case 'company':
				$article = M('article');
				$result = $article->where('cat_id=1')->field('article_id,title,add_time,click')->order('article_id desc')->select();

				foreach ($result as $k => $v) {
					$result[$k] = $v;
    				$result[$k]['add_time'] = date('Y-m-d',$result[$k]['add_time']);
				}

				echo json_encode($result);	
				break;
			case 'industry':
				$article = M('article');
				$result = $article->where('cat_id=2')->field('article_id,title,add_time,click')->order('article_id desc')->select();
				foreach ($result as $k => $v) {
					$result[$k] = $v;
    				$result[$k]['add_time'] = date('Y-m-d',$result[$k]['add_time']);
				}
				echo json_encode($result);	
				break;
		
		}

	}

	public function Pages(){

		$json = $GLOBALS['HTTP_RAW_POST_DATA'];
		$arrlist = json_decode($json, TRUE);

		switch ($arrlist['pages']) {
			case 'classify':
				$pages = M('pages');
				$result = $pages->where('id=1')->field('img')->select();
				$result = "https://project.csnet.net.cn".substr($result[0]['img'], 8);
				echo json_encode($result);	
				break;
			
			case 'about':
				$pages = M('pages');
				$result = $pages->where('id=2')->field('img')->select();
				$result = "https://project.csnet.net.cn".substr($result[0]['img'], 8);
				echo json_encode($result);
				break;

			case 'news':
				$pages = M('pages');
				$result = $pages->where('id=3')->field('img')->select();
				$result = "https://project.csnet.net.cn".substr($result[0]['img'], 8);
				echo json_encode($result);
				break;	
		}

	}

	public function Goodsselect(){
		$json = $GLOBALS['HTTP_RAW_POST_DATA'];
		$arrlist = json_decode($json, TRUE);
		$equipment = D('equipment');
		$equipment = $equipment ->where("equipment_id = {$arrlist['id']}") -> find();
		$equipment['img'] = "https://project.csnet.net.cn".substr( $equipment['img'], 8);
		echo json_encode($equipment);
		
	}

	//生成订单
	public function orderadd(){
		$json = $GLOBALS['HTTP_RAW_POST_DATA'];
		$arrlist = json_decode($json, TRUE);
		$order = D("order");
		date_default_timezone_set('PRC');
		$d = date('y-m-d h', time());
		$j = time();
		$sn = date('Ymdhis') . str_pad(mt_rand(1, 9), 4, '0', STR_PAD_LEFT); 
		$arrayName = array('order_sn' =>$sn,
			'equipment_name' => $arrlist['equipment_name'],
			'equipment_address' => $arrlist['equipment_position'],
			'equipment_code' => $arrlist['equipment_shortcode'],
			'equipment_assets' => $arrlist['equipment_assdtnumber'],
			'equipment_sequence' => $arrlist['equipment_sequencenumber'],
			'equipment_manufacturer' => $arrlist['equipment_manufacturer'],
			'equipment_state' => $arrlist['device_status'],
			'equipment_degree' => $arrlist['emergency_degree'],
			'equipment_standby' => $arrlist['spare_machine'],
			'equipment_fault' => $arrlist['symptom'],
			'equipment_instructions' => $arrlist['description'],
			'equipment_number' => $arrlist['phone'],
			'add_time' => $j
			
 );
		$id = 	$order -> add($arrayName);

	if($id){
		echo json_encode($sn);
	}
	}
	//保障订单生成
	public function uploadFile(){
		$json = $GLOBALS['HTTP_RAW_POST_DATA'];
		$arrlist = json_decode($json, TRUE);
		$order = D("order");
		        $cfg = array('rootPath' => './../api/upload/equipment/', 'autoSub' => false, );
	            $upload = new \Think\Upload($cfg);
	                // 图片上传
	            $result = $upload -> upload($_FILES);
	            $bigpathname = $upload -> rootPath;

	                if (!$result) {// 上传错误提示错误信息
	                   // $this -> error($upload -> getError());

	                } else {// 上传成功 获取上传文件信息
	                    foreach ($result as $file) {
	                        //图片地址
	                        $dizi = $bigpathname . $file['savepath'] . $file['savename'];
	                    }
	                }
	              // dump($_POST['order']);
	                $orderlist = $order -> where("order_sn = '{$_POST['order']}'") ->find();
					$img = $orderlist['equipment_img'];
	                if(substr($dizi, -3) == "mp3"){
	                	$arrayName = array('equipment_recording' =>$dizi );
	                	$orderlist = $order -> where("order_sn = '{$_POST['order']}'") ->save($arrayName);
	                }elseif (empty($img)) {
	                	$equipment_img = $dizi;
						$arrayName = array('equipment_img' =>$dizi );

						$orderlist = $order -> where("order_sn = '{$_POST['order']}'") ->save($arrayName);
	                }else{
						$equipment_img = $img .",".$dizi;
						$arrayName = array('equipment_img' =>$equipment_img );
						$orderlist = $order -> where("order_sn = '{$_POST['order']}'") ->save($arrayName);


		}

		

		// if(empty($img) ){//判断图片是否已经有保存
		// 		$equipment_img = $dizi;
		// 		$arrayName = array('equipment_img' =>$dizi );

		// 		$orderlist = $order -> where("order_sn = '{$_POST['order']}'") ->save($arrayName);
		// }else{
		// 	$equipment_img = $img .",".$dizi;
		// 	$arrayName = array('equipment_img' =>$equipment_img );
		// 	$orderlist = $order -> where("order_sn = '{$_POST['order']}'") ->save($arrayName);


		// }

	}
}